{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Querying the Results of Tasks (Electrons) in a Workflow\n",
    "\n",
    "Query the result values of all electrons in a lattice at once or individually.\n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "Create and dispatch a lattice."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "\n",
    "@ct.electron\n",
    "def add(x, y):\n",
    "    return x + y\n",
    "\n",
    "@ct.electron\n",
    "def multiply(x, y):\n",
    "    return x * y\n",
    "\n",
    "@ct.lattice\n",
    "def workflow(x, y):\n",
    "    res_1 = add(x=x, y=y)\n",
    "    return multiply(x=res_1, y=y)\n",
    "\n",
    "dispatch_id = ct.dispatch(workflow)(x=2, y=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Procedure\n",
    "\n",
    "1. Retrieve the workflow result object using the Covalent `get_result()` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = ct.get_result(dispatch_id=dispatch_id, wait=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Query the electron executions using the `get_all_node_ouputs()` method. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'add(0)': <covalent.TransportableObject at 0x11a79d400>,\n",
       " ':parameter:2(1)': <covalent.TransportableObject at 0x11a79d8e0>,\n",
       " ':parameter:3(2)': <covalent.TransportableObject at 0x11a79de50>,\n",
       " 'multiply(3)': <covalent.TransportableObject at 0x11aab7640>,\n",
       " ':parameter:3(4)': <covalent.TransportableObject at 0x11ab01280>}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.get_all_node_outputs()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Note the ID number of the nodes in which you are interested. The node ID is in parentheses after the node name.\n",
    "\n",
    "   The list of nodes contains both electrons and parameters. In this example, node 0 and node 3 (`add` and `multiply`, respectively) are electrons.\n",
    "   \n",
    "4. Use the `result.get_node_result()` function to examine the result of an individual node."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'node_id': 0,\n",
       " 'node_name': 'add',\n",
       " 'start_time': datetime.datetime(2023, 2, 1, 21, 9, 0, 618814, tzinfo=datetime.timezone.utc),\n",
       " 'end_time': datetime.datetime(2023, 2, 1, 21, 9, 0, 680075, tzinfo=datetime.timezone.utc),\n",
       " 'status': Status(STATUS='COMPLETED'),\n",
       " 'output': <covalent.TransportableObject at 0x11a79d400>,\n",
       " 'error': None,\n",
       " 'sublattice_result': None,\n",
       " 'stdout': '',\n",
       " 'stderr': ''}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.get_node_result(node_id=0)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### See Also\n",
    "\n",
    "[Querying the Status of an Electron](../status/query_electron_execution_status.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
